package com.hangzhou.xyj.db;

import android.content.Context;

import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.utils.StringUtil;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import java.sql.SQLException;
import java.util.List;

/**
 * @author : neo.duan
 * @date : 	  2016年2月23日
 * @desc : 加群组信息的数据库操作
 */

public class GroupPushMsgDao {
    private Dao<GroupPushMsgModel, Integer> mDao;
    private DataHelper mHelper = null;

    public GroupPushMsgDao(Context context) {
        this.mHelper = OpenHelperManager.getHelper(context, DataHelper.class);
        try {
            this.mDao = mHelper.getDao(GroupPushMsgModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加或更新用户
     *
     * @param info
     */
    public void add(GroupPushMsgModel info) {
        if (info == null) return;
        try {
            mDao.createOrUpdate(info);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据用户id获取当前用户信息
     *
     * @param uid
     * @return
     */
    public GroupPushMsgModel getPushMsg(String uid) {
        if (StringUtil.isBlank(uid)) return null;
        GroupPushMsgModel info = null;
        try {
            List<GroupPushMsgModel> msgList = mDao.queryBuilder().where().eq(GroupPushMsgModel.TOID, uid).query();
            if (msgList == null || msgList.isEmpty()) {
                info = new GroupPushMsgModel();
            } else {
                info = msgList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 根据用户id获取系统消息
     *
     * @return
     */
    public List<GroupPushMsgModel> getAllMessage(String uid) {
        if (StringUtil.isBlank(uid))
            return null;

        List<GroupPushMsgModel> msgs = null;

        try {
            QueryBuilder<GroupPushMsgModel, Integer> builder = mDao.queryBuilder().orderBy(GroupPushMsgModel.MSG_TIME, false);
            builder.where().eq(GroupPushMsgModel.TOID, uid);
            msgs = builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return msgs;
    }


    /**
     * 删除消息
     *
     * @param id
     */
    public void delete(int id) {
        try {
            DeleteBuilder<GroupPushMsgModel, Integer> builder = mDao.deleteBuilder();
            builder.where().eq(GroupPushMsgModel.ID, id);
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除消息
     *
     * @param uid
     */
    public void delete(String uid) {
        try {
            DeleteBuilder<GroupPushMsgModel, Integer> builder = mDao.deleteBuilder();
            builder.where().eq(GroupPushMsgModel.TOID, uid);
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询未读消息数
     *
     * @return
     */
    public int getUnReadCount(String uid) {
        try {
            QueryBuilder<GroupPushMsgModel, Integer> builder = mDao.queryBuilder().orderBy(GroupPushMsgModel.MSG_TIME, false);
            builder.where().eq(GroupPushMsgModel.IS_READED, false).and().eq(GroupPushMsgModel.TOID, uid);
            return (int) builder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 批量更新是否已读
     *
     * @param uid
     */
    public void updateIsRead(String uid) {
        if (StringUtil.isBlank(uid))
            return;
        try {
            UpdateBuilder<GroupPushMsgModel, Integer> builder = mDao.updateBuilder();
            builder.updateColumnValue(GroupPushMsgModel.IS_READED, true);
            builder.where().eq(GroupPushMsgModel.TOID, uid);
            builder.update();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
